﻿package com.saint.bitmapDistortion.animations 
{
	import caurina.transitions.Tweener;
	import com.saint.bitmapDistortion.data.AnimationPointData;
	import com.saint.bitmapDistortion.IStretchyImage;
	import com.saint.bitmapDistortion.StretchyImage;
	import com.saint.utils.DistanceBetweenPoints;
	import com.saint.utils.Logger;
	import flash.geom.Point;
	
	
	
	/**
	 * animates a stretchy image as if it is sucked into a click on the page
	 * 
	 * 	m
	 * 
	 * 
	 * @author kembera
	 */
	public class SuckToPoint implements IStretchyAnimation
	{
		private var _image:IStretchyImage;
		private var _targetPoint:Point;
		private var _delayTime:Number;
		
		private var animationPoints:Array = new Array();
		private var orderedPoints:Array = new Array();
		
		private var transition:String = "easeInQuint";
		//private var transition:String = "easeInCubic";
		
		
		/**
		 * CONTRUCTOR
		 * 
		 * @param	image
		 */
		public function SuckToPoint(image:IStretchyImage, targetPoint:Point, delayTime:Number = 1) 
		{
			Logger.info("SuckToPoint.SuckToPoint " + targetPoint.x + ", " + targetPoint.y);	
			/* set stretchy image */
			_image = image;
			
			/* set target point */
			_targetPoint = targetPoint;			
			
			/* set the speed */
			_delayTime = delayTime;
			
			/* get animation points */
			animationPoints = getAnimationPoints();			
			
			/* order the points in according to distance from click (closest to farthest)*/
			orderedPoints = calculateTweenOrder(animationPoints);			
			
			
			/* calculate the delay perc */
			calculateDelayPerc(orderedPoints);
			
			//trace("")
			//for (var i:Number = 0 ; i < orderedPoints.length ; i++)
			//{
				//trace(orderedPoints[i].animationPoint.name + ", " +  orderedPoints[i].distance);
			//}
			
			/* start the animation */			
			startAnimation();

			
		}
		
		private function calculateDelayPerc(dataArray:Array):void
		{
			var minVal:Number = AnimationPointData(dataArray[0]).distance;
			var maxVal:Number = AnimationPointData(dataArray[dataArray.length - 1]).distance;
			var distanceRange:Number = maxVal - minVal;
			var perc:Number;
			for (var i:Number = 0; i < dataArray.length; i++)
			{
				var val1:Number = AnimationPointData(dataArray[i]).distance - minVal;
				perc = val1 /  distanceRange;
				trace(i + ", " + perc);
				AnimationPointData(dataArray[i]).delay = perc;
			}
			
		}
		
		/**
		 * CALCULATES WHICH ANIMATION POINTS ARE NEAREEST TO THE DESTINATION POINT
		 * 
		 * AND ORDERS THEM IN ORDER OF CLOSESET TO FARTHERST AND 
		 */
		private function calculateTweenOrder(animPoints:Array):Array
		{
			Logger.info("SuckToPoint.calculateTweenOrder");
			
			var globalPoint:Point;
			var localPoint:Point;
			var arr:Array = new Array();
			var distance:Number;
			
			Logger.debug("SuckToPoint.calculateTweenOrder startLoop");
			for (var i:Number = 0; i < animPoints.length; i++)
			{
				Logger.debug("SuckToPoint.calculateTweenOrder " + i );	
				localPoint= new Point(animPoints[i].x, animPoints[i].y);
				Logger.debug("SuckToPoint.calculateTweenOrder a")
				globalPoint = new Point();
				Logger.debug("SuckToPoint.calculateTweenOrder b")
				globalPoint = _image.localToGlobal(localPoint);		
				Logger.debug("SuckToPoint.calculateTweenOrder d")
				distance = DistanceBetweenPoints.calculateDistance(_targetPoint, globalPoint);				
				Logger.debug("SuckToPoint.calculateTweenOrder e")
				arr.push(new AnimationPointData(animPoints[i], distance));
			}
			
			Logger.debug("SuckToPoint.calculateTweenOrder f")			
			arr = arr.sortOn("distance", Array.NUMERIC);
			return arr;
		}

		
		/**
		 * GETS THE POINTS TO ANIMATE
		 */
		private function getAnimationPoints():Array
		{
			Logger.info("SuckToPoint.getAnimationPoints");
	
			var points:Array = _image.buttons;
			return points;			
		}
		
		
		
		/**
		 * animate in order of distance form the point
		 * 
		 * 
		 *TODO - MAKE THE DELAY RELATIVE TO THE DISTANCE
		 */
		public function startAnimation():void
		{
			Logger.info("SuckToPoint.startAnimation");
			
			var totalDelayStart = _delayTime;			
			var delayStep:Number = 0.2;
			var delayTime:Number;		
			var targetY:Number = _targetPoint.y - _image.y;
			var targetX:Number = _targetPoint.x - _image.x;
			
			for (var i:Number = 0 ; i < orderedPoints.length ; i++)
			{
	
				delayTime =  AnimationPointData(orderedPoints[i]).delay * totalDelayStart;
				Tweener.addTween(AnimationPointData(orderedPoints[i]).animationPoint, { time:1, y: targetY , x:targetX, transition:transition , delay: delayTime} );
			}
		
		}
		
		
	}

}